package T4.EjemplosDeIO;

import java.io.*;
import java.util.*;

public class Ejemplo2 {

    static public void main(String[] args) {
        File file1 = new File("C:/datos.dat");
        File file2 = new File("C:/objetos.dat");
        FileOutputStream fileOutputStream = null, fileOutputStream2 = null;
        DataOutputStream dataOutputStream = null;
        ObjectOutputStream objectOutputStream = null;

        try {
            fileOutputStream = new FileOutputStream(file1);
            dataOutputStream = new DataOutputStream(fileOutputStream);

            dataOutputStream.writeBoolean(true);
            dataOutputStream.writeInt(45);
            dataOutputStream.writeDouble(4.8);
            fileOutputStream2 = new FileOutputStream(file2);
            objectOutputStream = new ObjectOutputStream(fileOutputStream2);
            objectOutputStream.writeObject(new Date());

        } catch (IOException ex) {
            System.out.println("Se ha producido un error antes de terminar las escrituras");
            ex.printStackTrace();
        } finally {
            if (dataOutputStream != null) {
                try {
                    dataOutputStream.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (fileOutputStream2 != null) {
                try {
                    fileOutputStream2.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
